package com.aquila.datacontrol;

import java.util.ArrayList;

import com.aquila.datacontrol.object.BelongObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
 * 读取数据的基础类
 * @author Administrator
 *
 */
public class BelongDataSqlite extends SQLiteOpenHelper {
	public static final String TABLE_NAME = "Books";
	public static final int Version =1;
	private SQLiteDatabase db;
	private String KEY_BookID = "BookID" ;
	private String KEY_Name= "Name" ;
	private String KEY_EnName = "EnName" ;
	private String KEY_Tag= "Tag" ;
	private String KEY_PartCode = "PartCode" ;
	private String KEY_ChapterCount= "ChapterCount" ;
	
	/***
	 * 创建数据库表
	 * @param db
	 *2012-7-30 下午1:15:53
	 */
	private void createTable(SQLiteDatabase db){
		String sqlstr = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( "+
				KEY_BookID +" INTEGER, "+
				KEY_Name + "  VARCHAR2(50), "+
				KEY_EnName+ "  VARCHAR2(50), "+
				KEY_Tag + "  VARCHAR2(255), "+
				KEY_PartCode+ "  CHAR(20), "+
	            KEY_ChapterCount + " INTEGER);";
		try{
			db.execSQL(sqlstr);
		}catch (SQLException e) {
			e.printStackTrace() ;
		}
	}
	private Context context ;
	public BelongDataSqlite(Context context) {
		super(context, SystemConfig.DATABASE_NAME, null, Version);
		this.context = context ;
		db = getWritableDatabase() ;
		createTable(db) ;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}
	
	/***
	 * 打开数据库
	 *2012-7-30 下午1:37:13
	 */
	public void open() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase() ;
		}
	}
	/**
	 * 关闭数据库
	 */
	public void close(){
		if (db.isOpen()) {
			db.close() ;
		}
	}
	
	
	/**
	 * 插入一条数据
	 * @param object
	 *2012-8-8 上午10:09:36
	 */
	public boolean insertData(BelongObject object){
		if (object == null) {
			return false;
		}
			ContentValues contentValues = inputContentValues(object) ;
			db.insert(TABLE_NAME, null, contentValues) ;
			return true;
	}
	
	
	public BelongObject getBelongObject(int bookIndex){
		String sqlString = "select * from "+TABLE_NAME+" where "+
				KEY_BookID +" = '" + bookIndex  +"'";
		Cursor cursor = db.rawQuery(sqlString, null) ;
		BelongObject object = new BelongObject() ;
		if (cursor!= null && cursor.getCount()>0) {
        	cursor.moveToFirst() ;
        	object = readDataResultObject(cursor) ;
        	cursor.close() ;
        }
		return object ;
	}
	
	
	/**
	 * 封装读取数据的方法
	 * @param cursor
	 * @return
	 *2012-8-8 上午10:00:08
	 */
	private BelongObject readDataResultObject(Cursor cursor){
		BelongObject object= new BelongObject() ;
		object.setBookID(cursor.getInt(cursor.getColumnIndex(KEY_BookID))) ;
		object.setChapterCount(cursor.getInt(cursor.getColumnIndex(KEY_ChapterCount))) ;
		object.setEnName(cursor.getString(cursor.getColumnIndex(KEY_EnName))) ;
		object.setName(cursor.getString(cursor.getColumnIndex(KEY_Name))) ;
		object.setPartCode(cursor.getString(cursor.getColumnIndex(KEY_PartCode))) ;
		object.setTag(cursor.getString(cursor.getColumnIndex(KEY_Tag))) ;
		return object ;
	}
	
	
	/**
	 * 存数据封装的方法
	 * @param object
	 * @return
	 *2012-8-8 上午9:45:14
	 */
	private ContentValues inputContentValues(BelongObject object){
		ContentValues cv = new ContentValues() ;
		cv.put(KEY_BookID, object.getBookID()) ;
		cv.put(KEY_ChapterCount, object.getChapterCount());
		cv.put(KEY_EnName, object.getEnName()) ;
		cv.put(KEY_Name, object.getName()) ;
		cv.put(KEY_PartCode, object.getPartCode()) ;
		cv.put(KEY_Tag, object.getTag()) ;
		return cv ;
	}
	
	
}
